package com.zhengshang.api.task.payorder;

import com.alipay.api.AlipayApiException;
import com.github.binarywang.wxpay.bean.result.WxPayOrderCloseResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.zhengshang.api.domain.AppPayRecord;
import com.zhengshang.api.service.IAppPayRecordService;
import com.zhengshang.api.utils.WxPayUtils;
import com.zhengshang.common.constant.app.AppConstant;
import com.zhengshang.common.enums.UserStatus;
import com.zhengshang.common.pay.alipay.AlipayService;
import com.zhengshang.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CancelPayOrder implements Runnable {

    private IAppPayRecordService appPayRecordService;
    private AlipayService alipayService;
    private static final Logger log = LoggerFactory.getLogger(CancelPayOrder.class);

    public CancelPayOrder(IAppPayRecordService appPayRecordService, AlipayService alipayService) {
        this.appPayRecordService = appPayRecordService;
        this.alipayService = alipayService;
    }


    @Override
    public void run() {
        setWaitOrderList();
        System.out.println("待付款支付订单加入队列成功！");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        while (true) {
            try {
                AppPayRecord appPayRecord = (AppPayRecord) PayOrderCollection.getOrderCollection().take();

                if (PayOrderCollection.getDelay(appPayRecord.getOrderNum()) == -1) {
                    if ("1".equals(appPayRecord.getPayType())) {
                        //支付宝
                        alipayService.closeOrder(appPayRecord.getOrderNum());
                    } else if ("2".equals(appPayRecord.getPayType())) {
                        //微信
                        WxPayUtils.getWxPayService().closeOrder(appPayRecord.getOrderNum());
                    } else {
                        continue;
                    }
                    appPayRecord.setStatus("40");
                    appPayRecord.setUpdateTime(DateUtils.getNowDate());
                    appPayRecord.setUpdateBy("admin");
                    appPayRecordService.cancelOrder(appPayRecord);
                    log.info(sdf.format(new Date()) + "订单ID为：" + appPayRecord.getOrderNum() + "的订单超时已关闭。");
                    PayOrderCollection.getOrderCollection().remove(appPayRecord);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                log.error("CancellOrder DelayQueue 错误 {}", e.getMessage());
            } catch (AlipayApiException e) {
                e.printStackTrace();
                log.error("支付宝关闭待支付订单失败，{}", e.getMessage());
            } catch (Exception e) {
                e.printStackTrace();
                log.error("关闭待支付订单失败，{}", e.getMessage());
            }
        }
    }

    /**
     * 将未取消的待支付订单放入list中
     * @return
     */
    public void setWaitOrderList(){
        AppPayRecord appPayRecordQuery = new AppPayRecord();
        appPayRecordQuery.setStatus("10");
        appPayRecordQuery.setDelFlag(UserStatus.OK.getCode());
        List<AppPayRecord> list = appPayRecordService.selectAppPayRecordList(appPayRecordQuery);
        for (AppPayRecord appPayRecord : list) {
            appPayRecord.setWaitData();
            PayOrderCollection.getOrderCollection().add(appPayRecord);
        }
    }
}
